articles

Home / DeveloperSection / Articles / Interface in oops

Interface in oops

Anonymous User1456 14-Nov-2018

INTERFACE IN C#    

We can assume the interface type almost the same as the abstract base class. The way in which an abstract class is required to be derived, it is important to implement any interface in any class or structure in the same way.

Interfaces in C # provide a way to achieve runtime polymorphism. Using the interface, we can invite the functions of different sections through a single interface reference, while using virtual functions, and we can invite different sections of tasks in the same heritage (Inheritance) hierarchy through the same context.

Interfaces are like abstract classes and they share the fact that none of them can be created. However, interfaces are more conceptual than abstract classes, because any method bodies are not allowed.

So an interface is like an intangible class which is nothing but abstract methods, and since there is no method with the actual code, therefore no field is required. Properties are allowed, as well as index and events.

If suppose that we are considering an interface as a contract - a class that implements it is required to implement all of the methods and properties. However, the most important difference is that while C# doesn't allow multiple inheritances, where classes inherit more than a single base class, it does, in fact, allow for the implementation of multiple interfaces.

Interface in oops

The interface looks like a class but has no implementation.

• The only thing it contains is declarations of events, indexers, methods and/or properties.

• The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared.

The Interfaces in C# are provided as a replacement of multiple inheritances.

Because C# does not support multiple inheritances, it was necessary to incorporate some other method so that the class can inherit the behavior of more than one class, avoiding the puzzle of name opacity that is found in C++.

Where with the name opacity, the object of a class does not know which method to call if the two base classes of that class object contain the same named method.

Interface in oops

Purposes of Interfaces

• create loosely coupled software

• support design by contract (an implementer must provide the entire interface)

• allow for pluggable software

• allow different objects to interact easily

• hide implementation details of classes from each other

• facilitate reuse of software

Interface in oops

Interface एक User Defined Reference Type होता है, जो कि Methods के एक समूह को Specify तो करता है,

लेकिन उन्हें Implement नहीं करताए बल्कि जो Classes इन Interfaces को Use करते हैं, उन Classes में Interface के सभी Declared Methods को Implement करना होता है।

सरल शब्दों में कहें तो Interface Type, Abstract Members का एक Named Set होता है।


Interface एक प्रकार से Multiple Inheritance व Polymorphism की सुविधा Provide करने का काम करता है।

क्योंकि C# भी Java की तरह ही Object Oriented Programming System के Multiple Inheritance Concept को Directly Support नहीं करता।

In an Interface Definition, we can not only specify Data Members and Static Members. It can only specify Declaration or Prototype of Non-Static Methods, Properties, Events, and Indexers Because all these are Internally Methods.

Also, in these interfaces, there is no Definition or Implementation of these Methods, but in the Class or Structure, these Methods have to be used, among them all Declared Methods of Interface are implemented.

An Example for Interface :

using System;
using System.Collections.Generic;
namespace Interfaces
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Dog> dogs = new List<Dog>();
            dogs.Add(new Dog("Jaiki"));
            dogs.Add(new Dog("Bob"));
            dogs.Add(new Dog("Tiger"));
            dogs.Sort();
            foreach (Dog dog in dogs)
                Console.WriteLine(dog.Describe());
            Console.ReadKey();
        }
    }
    interface IAnimal
    {
        string Describe();
        string Name
        {
            get;
            set;
        }
    }
    class Dog : IAnimal, IComparable
    {
        private string name;
        public Dog(string name)
        {
            this.Name = name;
        }
        public string Describe()
        {
            return "This is a pet dog and name is " + this.Name;
        }
        public int CompareTo(object obj)
        {
            if (obj is IAnimal)
                return this.Name.CompareTo((obj as IAnimal).Name);             return 0;
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
}

Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By